@Profile

该注解配合@Configuration可以使用java代码指定不同profile的配置,可以注解在类上,也可以注解在@Bean的方法上。

注解在类上

dev

@Configuration
@Profile("dev")
public class CacheConfigDev {

    private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.class);

    @Bean
        public CacheManager concurrentMapCacheManager() {
        log.debug("Cache manager is concurrentMapCacheManager");
                return new ConcurrentMapCacheManager("movieFindCache");
        }

}

prod

@Configuration
@Profile("prod")
public class CacheConfigProd {

    private static final Logger log = LoggerFactory.getLogger(CacheConfigProd.class);

    @Bean
    public CacheManager cacheManager() {
        log.debug("Cache manager is ehCacheCacheManager");
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
        cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cmfb.setShared(true);
        return cmfb;
    }

}

注解在@Bean上

@Configuration
public class ProfileConfig {

    @Bean(name="dev")
    @Profile("dev")
     DevProfile devProfile() {
        return new DevProfile();
    }

    @Bean(name="prod")
    @Profile("prod")
     ProdProfile ProdProfile() {
        return new ProdProfile();
    }
}

docs


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...